package com.woniu.userassembly.identifycontroller.impl;





import com.woniu.userassembly.identifycontroller.util.Base64Util;
import com.woniu.userassembly.identifycontroller.util.FileUtil;
import com.woniu.userassembly.identifycontroller.util.HttpUtil;

import java.net.URLEncoder;

//身份证识别

public class IdCard {
    public static String idcard(String filePath, String noodles) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            // 本地文件路径
            //String filePath = "E:\\testscxz\\QQ截图20201130155857.png";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            // front身份证正面/back身份证背面
            String param = "id_card_side=" + noodles + "&image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token（30分钟过期），线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            // 设置识别类型：https://console.bce.baidu.com/ai/#/ai/imagerecognition/app/edit~appId=2073988
            //String accessToken = "24.6f9db24a016938b9e2038ce4db662457.2592000.1609317027.282335-23068058";
            String accessToken = AuthService.getaccessToken();

            String result = HttpUtil.post(url, accessToken, param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
